<?php

if (!defined('BASEPATH'))
	exit('No direct script access allowed');

/*
 * To change this template, choose Tools | Templates
* and open the template in the editor.
*/

/**
 * Description of category
 *
 * @author Rahul Sinha (rahul4u1994@gmail.com)
 */
class Location_management extends MX_Controller {

	//put your code here

	public function __construct() {
		parent::__construct();

		$this->load->model(array('admin_commoncall','admin_settings_model','admin_location_model','admin_counter_model'));

		//$this->admin_commoncall->check_admin_login();

		$this->load->helper('url');
		//$this->load->library('pagination');
		$this->load->library('pagination');
		//$this->load->model('admin_counter_model');
		$this->template->set_theme('admin');
		$this->template->set_layout('default');

	}
	public function index(){

		//print "HELLO";
		//die();
		//$data['country']['all'] = $this->admin_location_model->country_list();
		$data['state']['all'] = $this->admin_location_model->state_list();

		$this->template->title('Welcome', 'Ypage.com')
		->set_partial('nav_bar','layouts/nav_bar')
		->set_partial('side_menu','layouts/side_menu')
		//->set_partial('footer', 'footer/footer')
		->build('location/location_management',$data);
	}
	function addstate()
	{
		$data['mode'] = "ADD";

		$this->template->title('Welcome', 'Ypage.com')
		->set_partial('nav_bar','layouts/nav_bar')
		->set_partial('side_menu','layouts/side_menu')
		//->set_partial('footer', 'footer/footer')
		->build('location/add_state',$data);
	}

	function state_edit($state_id)
	{

		$data['mode'] = "EDIT";

		$data['state']['info'] = $this->admin_location_model->get_state($state_id);
		$this->template->title('Welcome', 'Ypage.com')
		->set_partial('nav_bar','layouts/nav_bar')
		->set_partial('side_menu','layouts/side_menu')
		//->set_partial('footer', 'footer/footer')
		->build('location/add_state',$data);
	}

	function insert_state() {
	
		$this->form_validation->set_rules('state_name', 'State Name', 'required|xss_clean|is_unique[country.country]');
		$this->form_validation->set_rules('url', 'Url', 'xss_clean');
		$this->form_validation->set_rules('meta_title', 'Meta Title', 'xss_clean');
		$this->form_validation->set_rules('meta_keyword', 'Meta Keyword', 'xss_clean');
		$this->form_validation->set_rules('meta_description', 'Meta Description', 'xss_clean');
	
		if ($this->form_validation->run()) {
			
			if($this->input->post('url') == FALSE){
				$url= $this->commoncall->url($this->input->post('state_name'));
				
				
			}else{
				$url= $this->commoncall->url($this->input->post('url'));
			}
			
			$post_data = array(
	
					'statename' => $this->input->post('state_name'),
					'url' => $url,
					'meta_title' => $this->input->post('meta_title'),
					'meta_keyword' => $this->input->post('meta_keyword'),
					'meta_description' => $this->input->post('meta_description')
						
			);
				
			//var_dump($post_data);
			//die();
				
			$this->db->insert('state',$post_data);
			$this->session->set_flashdata('message', "State Add Successfully");
			redirect('secure/location/location_management/addstate');
		}
			
		else{
			$this->session->set_flashdata('message', 'Type a State Name');
			redirect('secure/location/location_management/addstate');
		}
	
	
	}
	
	
	function update_state() {
	
		$this->form_validation->set_rules('state_name', 'State Name', 'required|xss_clean|is_unique[country.country]');
		$this->form_validation->set_rules('url', 'Url', 'xss_clean');
		$this->form_validation->set_rules('meta_title', 'Meta Title', 'xss_clean');
		$this->form_validation->set_rules('meta_keyword', 'Meta Keyword', 'xss_clean');
		$this->form_validation->set_rules('meta_description', 'Meta Description', 'xss_clean');
	
		if ($this->form_validation->run()) {
	
			$state_id =  $this->input->post('state_id');
			
			$post_data = array(
	
					'statename' => $this->input->post('state_name'),
					'url' => $this->input->post('url'),
					'meta_title' => $this->input->post('meta_title'),
					'meta_keyword' => $this->input->post('meta_keyword'),
					'meta_description' => $this->input->post('meta_description')
						
			);
				
			$this->db->where('id',$state_id);	
			$this->db->update('state',$post_data);
			$this->session->set_flashdata('message', "State Update Successfully");
			redirect('secure/location/location_management/state_edit/'.$this->input->post('state_id'));
		
		}
			
		else{
			$this->session->set_flashdata('message', 'Fields Can not null');
			redirect('secure/location/location_management/state_edit/'.$this->input->post('state_id'));
		}
	
	
	}
	
	function delete($state_id) {
		$this->admin_location_model->state_delete($state_id);
		redirect('secure/location/location_management');
	}
	
	
	
	function viewcity($state_id)
	{
		$state_id = $state_id;
		$data['state_id'] = $state_id;
		
		$data['city']['list'] = $this->admin_location_model->city_list($state_id);
		$this->template->title('Welcome', 'Ypage.com')
		->set_partial('nav_bar','layouts/nav_bar')
		->set_partial('side_menu','layouts/side_menu')
		//->set_partial('footer', 'footer/footer')
		->build('location/admin_city',$data);
	}
	
	function addcity($state_id)
	{
		
		$data['mode'] = "CITY ADD";
		
		$data['state_id'] = $state_id;
		
		$data['state_code'] = $this->admin_location_model->get_state_name($state_id);
		
		$this->template->title('Welcome', 'Ypage.com')
		->set_partial('nav_bar','layouts/nav_bar')
		->set_partial('side_menu','layouts/side_menu')
		//->set_partial('footer', 'footer/footer')
		->build('location/add_state',$data);
	}
	
	function city_edit($city_id,$state_id)
	{
		$data['mode'] = "CITY EDIT";
		
		$data['state_id'] = $state_id;
		
		$data['city']['info'] = $this->admin_location_model->get_city($city_id);
		
		$this->template->title('Welcome', 'Ypage.com')
		->set_partial('nav_bar','layouts/nav_bar')
		->set_partial('side_menu','layouts/side_menu')
		//->set_partial('footer', 'footer/footer')
		->build('location/add_state',$data);
	}
	
	function insert_city()
	{
		
		$this->form_validation->set_rules('city_name', 'State Name', 'required|xss_clean|is_unique[country.country]');
		$this->form_validation->set_rules('url', 'Url', 'xss_clean');
		$this->form_validation->set_rules('meta_title', 'Meta Title', 'xss_clean');
		$this->form_validation->set_rules('meta_keyword', 'Meta Keyword', 'xss_clean');
		$this->form_validation->set_rules('meta_description', 'Meta Description', 'xss_clean');
		$state_id = $_POST['state_id'];
		$state_code = $_POST['state_code'];
		
		$city_detail = $_POST['city_name'];
		
		if ($this->form_validation->run()) {
		
		if($this->input->post('url') == FALSE){
				$url= $this->commoncall->url($this->input->post('city_name'));
				
				
			}else{
				$url= $this->commoncall->url($this->input->post('url'));
			}
			
				
			$stname = $this->admin_location_model->get_state($state_id)->statecode;
			$cityName = $city_detail.", ".$stname;
			
			$post_data = array(
		
					'cityname' => $cityName,
					'stateid' => $state_id,	
					'statecode' => $state_code,
					'url' => $url,
					'meta_title' => $this->input->post('meta_title'),
					'meta_keyword' => $this->input->post('meta_keyword'),
					'meta_description' => $this->input->post('meta_description')
		
			);
		
			//var_dump($post_data);
			//die();
			
			$this->db->insert('city',$post_data);
			$this->session->set_flashdata('message', "City Add Successfully");
			redirect('secure/location/location_management/addcity/'.$state_id);
		
		}
			
		else{
			$this->session->set_flashdata('message', 'Fields Can not null');
			redirect('secure/location/location_management/addcity/'.$state_id);
		}
		
	}
	
	function update_city() {
	
		$this->form_validation->set_rules('city_name', 'CIty Name', 'required|xss_clean');
		$this->form_validation->set_rules('url', 'Url', 'xss_clean');
		$this->form_validation->set_rules('meta_title', 'Meta Title', 'xss_clean');
		$this->form_validation->set_rules('meta_keyword', 'Meta Keyword', 'xss_clean');
		$this->form_validation->set_rules('meta_description', 'Meta Description', 'xss_clean');
		
		$city_id = $_POST['city_id'];
		
		$state_id = $_POST['state_id'];
		
		if ($this->form_validation->run()) {
	
			$post_data = array(
	
					'cityname' => $this->input->post('city_name'),
					'stateid' => $state_id,
					'url' => $this->input->post('url'),
					'meta_title' => $this->input->post('meta_title'),
					'meta_keyword' => $this->input->post('meta_keyword'),
					'meta_description' => $this->input->post('meta_description')
	
			);
	
			$this->db->where('id',$city_id);
			$this->db->update('city',$post_data);
			$this->session->set_flashdata('message', "City Update Successfully");
			redirect('secure/location/location_management/city_edit/'.$city_id.'/'.$state_id);
	
		}
			
		else{
			$this->session->set_flashdata('message', 'Fields Can not null');
			redirect('secure/location/location_management/city_edit/'.$city_id.'/'.$state_id);
		}
	
	
	}
	
	function city_delete() {
		
		$city_id = $_GET['city_id'];
		$state_id = $_GET['state_id'];
		
		$this->admin_location_model->city_delete($city_id);
		redirect('secure/location/location_management/viewcity/'.$state_id);
	}
	
	function get_cities($stateID){
		header('Content-Type: application/x-json; charset=utf-8');
		echo(json_encode($this->admin_location_model->get_cities_by_state($stateID)));
		die();
	}
	
}
